package procuctconsumer;

import ch.qos.logback.core.net.SyslogOutputStream;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

class MyResource {

     private volatile  boolean flag=true;
     ArrayBlockingQueue<String> blockingQueue;
     private AtomicInteger atomicInteger =new AtomicInteger();
     public MyResource(ArrayBlockingQueue<String> blockingQueue) {
         this.blockingQueue =blockingQueue;
     }

     public void myProd() throws InterruptedException {
         while (flag){
            String data =atomicInteger.incrementAndGet()+"";
            boolean result =blockingQueue.offer(data,2L,TimeUnit.SECONDS);
            if (result){
                System.out.println(Thread.currentThread().getName()+"\t 插入队列："+data+"成功！");
            }else{
                System.out.println(Thread.currentThread().getName()+"\t 插入队列："+data+"失败！");
            }
             TimeUnit.SECONDS.sleep(1);
         }
         System.out.println(Thread.currentThread().getName()+"\t l老板喊停了，停止生产");

     }

     public void myCons() throws InterruptedException {
         while (flag){
           String result =  blockingQueue.poll(2L, TimeUnit.SECONDS);
           if (null==result||result.equals("")){
               System.out.println(Thread.currentThread().getName()+"\t 超过2秒没有购买到包子，退出消费。。。");
               return;
           }
             System.out.println(Thread.currentThread().getName()+"\t 购买包子成功:  "+result);
         }


     }

     public void  stop() throws InterruptedException {
         TimeUnit.SECONDS.sleep(5);
        flag=false;
     }
 }

public class ProdBlockQueueDemo {



    public static void main (String[] args) throws InterruptedException {
        MyResource myResource =new MyResource(new ArrayBlockingQueue<String>(5));

        new Thread(()->{
            System.out.println(Thread.currentThread().getName()+"\t 生成线程启动。。。");
            try {
                myResource.myProd();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"师傅A").start();


        new Thread(()->{
            System.out.println(Thread.currentThread().getName()+"\t 生成线程启动。。。");
            try {
                myResource.myProd();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"师傅B").start();

        new Thread(()->{
            System.out.println(Thread.currentThread().getName()+"\t 消费线程启动。。。");
            try {
                myResource.myCons();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"====顾客A").start();
        new Thread(()->{
            System.out.println(Thread.currentThread().getName()+"\t 消费线程启动。。。");
            try {
                myResource.myCons();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"====顾客B").start();
        new Thread(()->{
            System.out.println(Thread.currentThread().getName()+"\t 消费线程启动。。。");
            try {
                myResource.myCons();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"====顾客C").start();
        new Thread(()->{
            System.out.println(Thread.currentThread().getName()+"\t 消费线程启动。。。");
            try {
                myResource.myCons();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"====顾客D").start();

        myResource.stop();
    }
}
